const easeInOutQuad = function (t: number, b: number, c: number, d: number) {
	t /= d / 2;
	if (t < 1) {
		return (c / 2) * t * t + b;
	}

	t--;
	return (-c / 2) * (t * (t - 2) - 1) + b;
};

// RequestAnimationFrame for Smart Animating http://goo.gl/sx5sts
const requestAnimFrame = (function () {
	return (
		window.requestAnimationFrame
        || function (callback) {
        	window.setTimeout(callback, 1000 / 60);
        }
	);
})();

/**
 * Because it's so fucking difficult to detect the scrolling element, just move them all
 * @param {number} amount
 */
function move(amount: number) {
	document.documentElement.scrollTop = amount;
	(document.body.parentNode as any).scrollTop = amount;
	document.body.scrollTop = amount;
}

function position() {
	return (
		document.documentElement.scrollTop
        || (document.body.parentNode as any).scrollTop
        || document.body.scrollTop
	);
}

/**
 * @param {number} to
 * @param {number} duration
 * @param {Function} callback
 */
export const scrollTo = (to: number, duration: number, callback: (() => void) | undefined) => {
	const start = position();
	const change = to - start;
	const increment = 20;
	let currentTime = 0;
	duration = typeof duration === 'undefined' ? 500 : duration;
	const animateScroll = function () {
		// Increment the time
		currentTime += increment;
		// Find the value with the quadratic in-out easing function
		const val = easeInOutQuad(currentTime, start, change, duration);
		// Move the document.body
		move(val);
		// Do the animation unless its over
		if (currentTime < duration) {
			requestAnimFrame(animateScroll);
		} else if (callback && typeof callback === 'function') {
			// The animation is done so lets callback
			callback();
		}
	};

	animateScroll();
};
